package org.yang.database.orm.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.*;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 实体
 *
 * @author: lslands
 * @version: 1.0
 * @create: 2024/5/11
 * @description:
 */
@Getter
@Setter
@NoArgsConstructor
public abstract class BaseModel extends Model implements Serializable {

    /**
     * 主键ID
     * @mock 1
     * @JsonSerialize(using = ToStringSerializer.class)： 解决 Long类型丢失精度问题,但是返回的类型是String
     * @ExcelIgnore  导出时忽略该字段
     */
    @TableId(value = "id", type = IdType.AUTO)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;


    /**
     * 状态
     * @mock 1
     */
    private Integer enabled;

    /**
     * 逻辑删除标识
     * @mock 0
     */
    @TableLogic
    @JsonIgnore
    @TableField(value = "logic",fill = FieldFill.INSERT)
    private Integer logic;

    /**
     * 开启乐观锁
     * @mock 1
     */
    @Version
    @TableField(value = "revision",fill = FieldFill.INSERT_UPDATE)
    private Integer revision;

    /**
     * 创建人
     */
    @TableField(value = "created_by",fill = FieldFill.INSERT)
    private String createdBy;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
    @TableField(value = "created_time",fill = FieldFill.INSERT)
    private LocalDateTime createdTime;

    /**
     * 更新人
     */
    @TableField(value = "updated_by",fill = FieldFill.INSERT_UPDATE)
    private String updatedBy;

    /**
     * 更新时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
    @TableField(value = "updated_time",fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedTime;


    public BaseModel(Long id) {
        this.id = id;
    }
    public BaseModel(Integer revision) {
        this.revision = revision;
    }

    public BaseModel(Long id, Integer enabled, Integer revision) {
        this.id = id;
        this.enabled = enabled;
        this.revision = revision;
    }

    public BaseModel(Integer revision, String updatedBy, LocalDateTime updatedTime) {
        this.revision = revision;
        this.updatedBy = updatedBy;
        this.updatedTime = updatedTime;
    }

    public BaseModel(Long id, Integer revision) {
        this.id=id;
        this.revision=revision;
    }
}
